using System;
using SiemensAmd.Mmt.McisDbModel;
using SqlSugar;
using SqlSugarEx.Utilities;

namespace SiemensAmd.Mmt.Amd.McisDbDal
{
	public class TdiToolDataDetailsDal
	{
		private readonly SqlSugarClient _sqlSugar;

		public TdiToolDataDetailsDal(string server, string db, string user, string pwd)
		{
			this._sqlSugar = new SqlSugarClient(new ConnectionConfig
			{
				DbType = DbType.SqlServer,
				ConnectionString = DbUtility.SQLDbFormat(server, db, user, pwd),
				IsAutoCloseConnection = false
			});
		}

		public Tuple<TDI_TOOLDATA_DETAILSEntity, TDI_TOOLPLACEEntity> UpdateUserData(string serverNode, string presetter, string dataFormat, long idTdiToolData)
		{
			string toolDataDetailsInfo = "";
			string toolDataData = "";
			TDI_TOOLDATA_DETAILSEntity toolDataDetailsEntity = null;
			TDI_TOOLPLACEEntity toolDataPlaceEntity = null;
			toolDataDetailsEntity = this._sqlSugar.Queryable<TDI_TOOLDATA_DETAILSEntity>().First((TDI_TOOLDATA_DETAILSEntity x) => x.DbIdTDIToolData == idTdiToolData);
			if (toolDataDetailsEntity != null)
			{
				toolDataDetailsInfo = toolDataDetailsEntity.ToolData;
				toolDataPlaceEntity = this._sqlSugar.Queryable<TDI_TOOLPLACEEntity>().First((TDI_TOOLPLACEEntity x) => x.DbIdTDIToolData == idTdiToolData);
				if (toolDataPlaceEntity != null)
				{
					toolDataData = this._sqlSugar.Queryable<TOOLDATAEntity>().First((TOOLDATAEntity x) => x.NODEID == serverNode && x.CONTAINERID == presetter && x.PLACENO == toolDataPlaceEntity.PlaceNo)?.DATA;
					if (!string.IsNullOrEmpty(dataFormat))
					{
						try
						{
							this.UpdateTdiToolDataDetail(ref toolDataDetailsInfo, dataFormat);
							this.ToolDataFormat(ref toolDataData, dataFormat);
							this._sqlSugar.Ado.BeginTran();
							(from x in this._sqlSugar.Updateable<TDI_TOOLDATA_DETAILSEntity>().UpdateColumns((TDI_TOOLDATA_DETAILSEntity x) => x.ToolData).ReSetValue(delegate(TDI_TOOLDATA_DETAILSEntity x)
								{
									x.ToolData = toolDataDetailsInfo;
								})
								where x.DbIdTDIToolDataDetails == toolDataDetailsEntity.DbIdTDIToolDataDetails
								select x).ExecuteCommand();
							(from x in this._sqlSugar.Updateable<TOOLDATAEntity>().UpdateColumns((TOOLDATAEntity x) => x.DATA).ReSetValue(delegate(TOOLDATAEntity x)
								{
									x.DATA = toolDataData;
								})
								where x.NODEID == serverNode && x.CONTAINERID == presetter && x.PLACENO == toolDataPlaceEntity.PlaceNo
								select x).ExecuteCommand();
							this._sqlSugar.Ado.CommitTran();
						}
						catch (Exception ex)
						{
							this._sqlSugar.Ado.RollbackTran();
							throw ex;
						}
					}
				}
			}
			return new Tuple<TDI_TOOLDATA_DETAILSEntity, TDI_TOOLPLACEEntity>(toolDataDetailsEntity, toolDataPlaceEntity);
		}

		public TDI_TOOLPLACEEntity QueryToolPlace(long idTdiToolData)
		{
			return this._sqlSugar.Queryable<TDI_TOOLPLACEEntity>().First((TDI_TOOLPLACEEntity x) => x.DbIdTDIToolData == idTdiToolData);
		}

		private void UpdateTdiToolDataDetail(ref string data, string dataFormat, UserDataEntity entity)
		{
			if (!data.Contains("\"TC_TPC\""))
			{
				data = data.Replace("\"DataFormat\":\"\"", "\"DataFormat\":\"" + dataFormat + "\"");
				int startIndex = data.IndexOf("\"TDI\":", StringComparison.Ordinal);
				string value = BodyBuild.UserDataFormat(entity);
				data = data.Insert(startIndex, value);
			}
		}

		private void UpdateTdiToolDataDetail(ref string data, string dataFormat)
		{
			data = data.Replace("\"DataFormat\":\"\"", "\"DataFormat\":\"" + dataFormat + "\"");
		}

		private void ToolDataFormat(ref string data, string dataFormat)
		{
			data = data.Replace("<ToolList>", "<ToolList dataFormat=\"" + dataFormat + "\">");
		}
	}
}
